﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TestDLL.domain;
using System.Collections;
using System.Data;

namespace TestDLL.dao
{
    public class TestDao : MSON.dao.AbstractDao
    {
        public TestDao()
        {
             string connectionString = MSON.Methods.getApplicationConfigSetting("TestConnectionString");
             base.connectionMySQL = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
        }

        private Hashtable getTestAsMap(Test test)
        {
            Hashtable map = new Hashtable();
            map.Add("name", test.getName());
            map.Add("value", test.getValue());
            return map;
        }

        public void updateTest(Test test)
        {
            Hashtable map = this.getTestAsMap(test);
            base.updateMySQL("SELECT * FROM tblTest WHERE test_id = " + test.getId(), map);
        }

        public int addTest(Test test) {
            Hashtable mapAdd = this.getTestAsMap(test);            
            return base.addMySQL("SELECT * FROM tblTest WHERE test_id = 0", mapAdd); 
        }

        public Test[] getAllTestItems()
        {
            return this.getAllTestItemsAsList().ToArray();
        }

        private List<Test> getAllTestItemsAsList()
        {
            List<Test> items = new List<Test>();
            string strSqlString = "SELECT * FROM tblTest";
            DataRowCollection rows = base.getSQLRows(strSqlString);
            foreach (DataRow row in rows)
            {
                items.Add(this.getTestFromDataRow(row));
            }
            return items;
        }

        private Test getTestFromDataRow(DataRow dr)
        {
            Test test = new Test();
            test.setName(Convert.ToString(dr["name"]));
            test.setValue(Convert.ToString(dr["value"]));
            test.setId(Convert.ToInt32(dr["test_id"]));
            return test;
        }

        public void deleteTestItem(int id)
        {
            string sqlString = "DELETE FROM tblTest WHERE test_id = " + id;
            base.executeMySQLCommand(sqlString);
        }

        public void updateTestItem(Test test)
        {

        }

    }
}
